﻿' Boogaloo® - http://www.mindfly.com
' Copyright (c) 2008
' by Mindfly, Inc.
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
' INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
' DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
' SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
' SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
' WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
' THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


Imports System.Web
Imports System.IO
Imports Boogaloo.Base
Imports Boogaloo.Core.BLL

Namespace HttpModules


    Public Class UrlRewrite
        Implements IHttpModule


#Region "HttpModule Members"



        Public Sub Dispose() Implements System.Web.IHttpModule.Dispose

        End Sub



        Public Sub Init(ByVal context As System.Web.HttpApplication) Implements System.Web.IHttpModule.Init

            AddHandler context.BeginRequest, AddressOf Context_BeginRequest

        End Sub



#End Region

        Public Sub Context_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
            Dim context As HttpContext = CType(sender, HttpApplication).Context

            Dim admin_path As String = String.Format("/{0}/", Globals.Settings.Components("Core").Settings("ADMIN_Directory").Value.ToUpper.Replace("/", String.Empty).Replace("~", String.Empty))

            Dim virtual_path_rewritten As String = context.Request.Path.ToUpper
            Dim virtual_directory As String = context.Request.ApplicationPath



            If Not String.Equals(virtual_directory, "/") Then virtual_path_rewritten = virtual_path_rewritten.Replace(virtual_directory.ToUpper, String.Empty)

            Dim instance_path As String = String.Format("{0}{1}", virtual_directory, virtual_path_rewritten)

            Dim file_exists As Boolean = File.Exists(context.Server.MapPath(instance_path))


            If virtual_path_rewritten.Contains(".ASPX") And Not virtual_path_rewritten.StartsWith(admin_path) And Not file_exists Then

                Dim host = Root.GetCurrentRoot()

                Dim page = Node.GetNodeByVirtualPathRewritten(host.root_id, virtual_path_rewritten)

                Try
                    If page IsNot Nothing Then context.RewritePath(String.Format("~/default.aspx?node={0}{1}", page.node_id.ToString, GetQueryString(context)), False)
                Catch ex As Exception
                    If Globals.Settings.EnableLogging Then
                        Dim rewrite As String = String.Format("/default.aspx?node={0}{1}", page.node_id.ToString, GetQueryString(context))
                        EventLogger.Write(virtual_path_rewritten.ToString & " -> " & rewrite & " (" & ex.Message & ")", "UrlRewrite", True)
                    End If
                End Try

            End If

        End Sub

        Private Shared Function GetQueryString(ByVal context As HttpContext) As String
            Dim qs As String = context.Request.QueryString.ToString
            If Not String.IsNullOrEmpty(qs) Then
                Return String.Format("&{0}", qs)
            Else
                Return String.Empty
            End If
        End Function

    End Class


End Namespace
